<div class="panel-base panel-admin">		
	<div class="row-fluid">
		<div class="span12">			
			<h1>Banner</h1>
			<p>แสดงรายการแบนเนอร์ทั้งหมด</p>
		</div>
	</div>
	<div class="panel-admin-inner">			
		<div class="navbar">
			<div class="navbar-inner">
				<div class="text-right">
					<a class="btn btn-primary" href="<?php print base_url(); ?>admin/banner/add">เพิ่มใหม่</a>
					<button class="btn">คำนวนเวลาใหม่</button>
				</div>
			</div>
		</div>	
		<div class="panel-message"></div>				
		<table class="table table-hover table-cafe">
			<thead>
				<tr>
					<th style="width:15%;"></th>	
					<th style="width:45%;"></th>
					<th style="width:10%;"></th>
					<th style="width:20%;"></th>																	
					<th style="width:10%;"></th>
				</tr>
			</thead>
			<tbody>			
				<?php
					if (count($data) > 0){
						for ($i=0; $i<count($data); $i++){ ?>							
							<tr>
								<td><img class="img-rounded thumbnail" src="<?php print $data[$i]['BAN_IMG']; ?>" style="width:70px; height:50px;" /></td>
								<td>			
									<span class="block">
										<?php if (!empty($data[$i]['BAN_CODE'])): ?>
											<b>Code Block</b>
										<?php else: ?>
											<?php if (!empty($data[$i]['BAN_URL'])): ?>
												<b><a href="<?php print $data[$i]['BAN_URL']; ?>" target="_blank"><?php print $data[$i]['BAN_URL']; ?></a></b>
											<?php else: ?>
												<b>NO URL</b>
											<?php endif; ?>
										<?php endif; ?>
									</span>						
									<span class="block">
										<small>											
											<?php print $data[$i]['BAN_P_TITLE']; ?> / 
											<?php if (!empty($data[$i]['BAN_CODE'])): ?>												
												ADS Adsense
											<?php else: ?>
												Banner
											<?php endif; ?>
										</small>
									</span>																
								</td>
								<td>									
									<div class="switch switch-mini switch-state" data-on="success" data-off="danger">
										<input type="hidden" name="id" value="<?php print $data[$i]['BAN_ID']; ?>" />										
										<?php if ($data[$i]['BAN_STATE']): ?>
									    	<input type="checkbox" checked />
									    <?php else: ?>
									    	<input type="checkbox"  />
									    <?php endif; ?>
									</div>
								</td>
								<td>
									<ul class="unstyled">
										<li><span class="full-row">วันที่: <?php print date('Y-m-d', strtotime($data[$i]['BAN_E_DATETIME'])); ?></span></li>
										<li><span class="full-row">คงเหลือ: <?php print ceil($data[$i]['BAN_REMAIN']); ?> วัน</span></li>
									</ul>
								</td>																		
								<td>
									<div class="btn-group">
										<button class="btn btn-mini dropdown-toggle" data-toggle="dropdown">Action <span class="caret"></span></button>
						                <ul class="dropdown-menu" style="left:-100px;">
						                	<li><a href="<?php print base_url() . 'admin/banner/view?i=' . $data[$i]['BAN_ID']; ?>"><i class="icon-list-alt"></i> รายละเอียด</a></li>
						                	<li><a href="#" class="btn-remove"><input type="hidden" value="<?php print $data[$i]['BAN_ID']; ?>" /><i class="icon-remove"></i> ลบข้อมูล</a></li>								                								                						                  
										</ul>
									</div>
								</td> 
							</tr>							
						<?php }
					} 
				?>
			</tbody>
		</table>		
	</div>	
	<div class="pagination">
		<ul>
			<li><a href="<?php print base_url() . 'admin/banner?p=1'; ?>">First</a></li>    
		    <?php for ($i=0; $i<$page_count; $i++) {
		    		if ($i == ($page_start_index-1)): ?>
		    			<li class="active"><a href="<?php print base_url() . 'admin/banner?p=' . ($i+1); ?>"><?php print $i+1; ?></a></li>
		    	<?php else: ?>
		      		<li><a href="<?php print base_url() . 'admin/banner?p=' . ($i+1); ?>"><?php print $i+1; ?></a></li>
		      	<?php endif; ?>
		    <?php }?>
			<li><a href="<?php print base_url() . 'admin/banner?p=' . $page_count; ?>">Last</a></li>
		</ul>
	</div>	
</div>
<script type="text/javascript">
	$(function(){
		$('.switch-state').on('switch-change', function (e, data) {		
			var i = $(this).find('input[name=id]').val();			   
		    $.post('<?php print base_url() . 'admin/banner/ajax_change_state'; ?>', { i : i, s : data.value }, function(output){
		    	  	
			    switch (output.result){
				    case -2: case -1:{				    	
				    	var html = '<div class="alert alert-error">';
						html += '<button type="button" class="close" data-dismiss="alert">&times;</button>';
						html += output.message + '</div>';
							
						$('.panel-message').html(html);
						setTimeout(function(){ window.location.href = output.redirect_url; },2000)
				    }
				    break;
			    }
		    }, 'json');
		});
		$('.btn-remove').click(function(){			
			create_confirm_model('<?php print base_url() . 'admin/banner/remove'; ?>', { i : $(this).find('input[type=hidden]').val() }, 'ยืนยันการลบข้อมูล', false);			
		});
	})
</script>
